package org.example.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.StudentMapper;
import org.example.pojo.PageResult;
import org.example.pojo.Student;
import org.example.pojo.StudentQueryParm;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class StudentServiceImp implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    @Override
    public PageResult<Student> findByPage(StudentQueryParm studentQueryParm) {
        //分页
        PageHelper.startPage(studentQueryParm.getPage(),studentQueryParm.getPageSize());
        List<Student> studentList = studentMapper.findByPage(studentQueryParm);
        Page<Student> p = (Page<Student>)studentList;
        return new PageResult<>(p.getTotal(),p.getResult());


    }

    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.insert(student);
    }

    @Override
    public Student findById(Integer id) {
        return  studentMapper.findById(id);

    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateViolation(Integer id, Short score) {
        //  违纪次数加一
        studentMapper.updateViolation(id,score);
        // 扣件纪律分

    }
}
